<?php
namespace app\index\controller;

use app\index\controller\Base;
use app\common\model\Apply as ApplyModel;
use app\common\model\Article as ArticleModel;
use app\common\model\Category as CategoryModel;
use app\common\model\Subscribe as SubscribeModel;

class Column extends Base
{
    public function initialize()
    {
        parent::initialize();
        $this->Model = new ArticleModel();
        $first_category_name = 'column';
        $this->CategoryModel = new CategoryModel();
        $category_tree = $this->CategoryModel->treeView(true);
        if(isset($category_tree[$first_category_name])){
          $tree_node = $category_tree[$first_category_name];
        }else{
          $tree_node = $this->CategoryModel->emptyTreeRootNode();
        }
        $this->tree_node = $tree_node;
        $this->assign('active_nav', $first_category_name);
    }

    public function index()
    {
        $param = [];
        $param = $this->request->param();
        $default_param = $this->index_default_param();

        $cat_id = 0;
        if(isset($this->tree_node['children'])){
            $categories = $this->tree_node['children'];
            if(isset($param['cat_id']) && isset($categories[$param['cat_id']])){
                $cat_id = $param['cat_id'];
            }else{
                if(isset($this->tree_node['default_second_category_id'])){
                    $cat_id = $this->tree_node['default_second_category_id'];
                }
            }
        }
        $default_param['cat_id'] = $cat_id;

        $res = $this->Model->userPaginate(array_merge($param, $default_param));
        $this->assign("tree_node", $this->tree_node);
        $this->assign("cat_id", $cat_id);
        $this->assign("res", $res);

        // 设置页面SEO信息
        $this->setPageSeo([
            'title' => $this->tree_node['title'],
            'description' => $this->tree_node['seo_description'],
            'keywords' => $this->tree_node['seo_keywords'],
        ]);
        return $this->fetch();
    }

    public function read($id = 0)
    {
        $article = $this->findValidArticle($id);
        if(empty($article)){
            $this->redirect('index');
        }

        $this->assign('article', $article);
        if($article['second_category_subed']){
            $tpl_name = 'read_sub';
        }else{
            $tpl_name = '';
        }

        if($article['second_category_applied']){
            // 属于可以申报项目的分类
            $ApplyModel = new ApplyModel;
            $old_apply = $ApplyModel->findUserArticleItem($id);
        }else{
            $old_apply = NULL;
        }
        $this->assign('old_apply', $old_apply);

        // 设置页面SEO信息
        $this->setPageSeo([
            'title' => $article['title'],
            'description' => $article['seo_description'],
            'keywords' => $article['seo_keywords'],
        ]);
        return $this->fetch($tpl_name);
    }

    //申请报名
    public function sub($id = 0){
        // 只有status=1的article可以报名
        $article = $this->findValidArticle($id, ['view_mode' => '']);
        $redirect_url = NULL;
        $redirect_msg = NULL;
        if(empty($article)){
            $redirect_url = url('index');
            $redirect_msg = '您报名的申请不存在';
        }else if($article['sub_state'] != 'processed'){
            $redirect_url = url('read', ['id' => $id]);
            $redirect_msg = '您报名的申请时间已结束';
        }

        if($this->request->isPost()){
            if(!is_null($redirect_url)){
                if($this->request->isAjax()){
                    $this->error($redirect_msg, $redirect_url);
                }else{
                    $this->redirect($redirect_url);
                }
            }

            $fields = [
                ['name' => 'user_name', 'type' => 'string', 'default' => 0],
                // ['name' => 'gender', 'type' => 'string', 'default' => ''],
                // ['name' => 'education', 'type' => 'string', 'default' => ''],
                // ['name' => 'email', 'type' => 'string', 'default' => ''],
                ['name' => 'mobile', 'type' => 'string', 'default' => ''],
            ];
            $default = [
                'article_id' => $id,
            ];
            $data = $this->parsePostParam($fields, $default);
            $SubscribeModel = new SubscribeModel();
            $res = $SubscribeModel->createOrUpdate($data);
            if($res["status"]){
                $this->success($res["msg"], url('read', ['id' => $id]));
            }else{
                $this->error($res["msg"]);
            }
        }else{
            if(!is_null($redirect_url)){
                $this->redirect($redirect_url);
            }
            // 设置页面SEO信息
            // $this->setPageSeo([
            //     'title' => $article['title'],
            //     'description' => $article['seo_description'],
            //     'keywords' => $article['seo_keywords'],
            // ]);
            $this->setPageSeo([
                'title' => '报名参加',
            ]);
            $this->redirect(url('read', ['id' => $id]));
        }
    }
}
